package com.isoft.permanprovidor.controller.sgman;

import com.isoft.permanprovidor.bean.ResponseData;
import com.isoft.permanprovidor.controller.BaseController;
import com.isoft.permanprovidor.entity.sgman.ContractWorker;
import com.isoft.permanprovidor.entity.sgman.ExterPerson;
import com.isoft.permanprovidor.service.sgman.ExterPerson_Service;
import com.isoft.permanprovidor.unit.FileUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/permanprovidor/exterperson")
public class ExterPerson_Controller extends BaseController {
    @Autowired
    ExterPerson_Service exterPerson_service;

    /*分页综合查询操作*/
    @PostMapping("/getpage")
    public ResponseData PageSynthSelect(Integer current , Integer size, String name){
        return exterPerson_service.PageSynthSelect(current,size,name);
    }
    /*新增外聘人员员工*/
    @PostMapping("/add")
    public ResponseData<Boolean> add(ExterPerson exterPerson){
        return returnBoolean(exterPerson_service.add(exterPerson));
    }

    /*批量导入外聘人员信息*/
    @Value("${myfile.user.exterPerson}")
    private String savePath;
    public ResponseData<String> bulkImport(@RequestParam("exterPerson") MultipartFile file , HttpServletRequest request){
        boolean result = false;
        String fileUrl = null;
        if (null != file){
            String newName="exterPerson.xlsx";
            fileUrl = FileUtil.uploadPhyPath(file , newName ,savePath , request);
            log.info("存入物理路径:" + fileUrl);
            if (null != fileUrl){
                try {
                    // 调用服务层方法处理上传的文件
                    exterPerson_service.bulkImport(fileUrl);
                    result = true;
                } finally {
                    // 无论成功或失败，尝试删除文件
                    File uploadedFile = new File(fileUrl);
                    if (uploadedFile.exists()) {
                        if (uploadedFile.delete()) {
                            log.info("暂存文件删除成功: " + fileUrl);
                        } else {
                            log.error("暂存文件删除失败: " + fileUrl);
                        }
                    }
                }
            }
        }
        log.info("文件上传路径:"+fileUrl);
        ResponseData<String> responseData = new ResponseData<>();
        responseData.setData(fileUrl);
        if (result){
            responseData.setErrorCode(200);
            responseData.setMsg("文件上传成功");
        }else {
            responseData.setErrorCode(605);
            responseData.setMsg("文件上传失败");
        }
        return responseData;
    }

    /*修改外聘人员信息*/
    @PutMapping("/update")
    public ResponseData<Boolean> update(@RequestBody ExterPerson exterPerson){
        return  returnBoolean(exterPerson_service.update(exterPerson));
    }
    /*启/停用该外聘人员*/
    @PostMapping("/freezeenable")
    public ResponseData<Boolean> islock (Integer id ,String sysuserid,Integer islock){
        return returnBoolean(exterPerson_service.islock(id, sysuserid, islock));
    }
}
